package cn.lanqiao.bigDate.service.sys.impl;

import cn.lanqiao.bigDate.entity.sys.SysMenu;
import cn.lanqiao.bigDate.entity.sys.SysRoleMenu;
import cn.lanqiao.bigDate.mapper.sys.SysMenuMapper;
import cn.lanqiao.bigDate.mapper.sys.SysRoleMenuMapper;
import cn.lanqiao.bigDate.reslut.TreeNode;
import cn.lanqiao.bigDate.service.sys.SysMenuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class SysMenuServiceImpl implements SysMenuService {
    @Resource
    private SysMenuMapper sysMenuMapper;

    @Resource
    private SysRoleMenuMapper sysRoleMenuMapper;
    @Override
    public  List<TreeNode> findAllSysMenu(SysMenu menu,String topName) {
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        //获取所有菜单
        List<SysMenu> sysMenus = sysMenuMapper.selectList(wrapper);
        //将菜单转换成树节点
        List<TreeNode> treeNodes = TreeNode.convertTreeNodes(sysMenus, topName);
        return treeNodes;
    }

    @Override
    public SysMenu findSysMenuOne(Long id) {
        //创建一个QueryWrapper的对象
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        SysMenu sysMenu = sysMenuMapper.selectOne(wrapper);
        return sysMenu;
    }

    @Override
    public TreeNode addSysMenu(SysMenu sysMenu) {
        sysMenu.setCreateTime(LocalDateTime.now());
        sysMenu.setUpdateTime(LocalDateTime.now());
        int insert = sysMenuMapper.insert(sysMenu);
        if(insert>0){
            return TreeNode.convertTreeNode(sysMenu);
        }
        return null;
    }

    @Override
    public boolean deleteSysMenu(Long id) {
        //删除菜单的同时需要删除菜单与角色关联信息和菜单关联的子菜单
        int delete = sysMenuMapper.deleteById(id);
        if(delete==1){
            return true;
        }
        return false;
    }

    @Override
    public TreeNode updateSysMenu(SysMenu sysMenu) {
        sysMenu.setUpdateTime(LocalDateTime.now());
        int insert = sysMenuMapper.updateById(sysMenu);
        if(insert>0){
            return TreeNode.convertTreeNode(sysMenu);
        }
        return null;
    }

    /**
     * 根据关联表中对应的id，使权限树在页面显示时自动勾选
     * @param roleId
     * @return
     */
    @Override
    public List<TreeNode> findAllSysMenu(Long roleId) {
        List<SysMenu> sysMenus;
        if(roleId!=null){
            QueryWrapper qw= new QueryWrapper();
            qw.eq("role_id",roleId);
            List<SysRoleMenu> list = sysRoleMenuMapper.selectList(qw);
            if(list!=null && list.size()>0){
                //讲list中的id取出来
                Set<Long> menuIds = list.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toSet());
                QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
                wrapper.orderByDesc("id");
                //获取所有菜单
                sysMenus= sysMenuMapper.selectList(wrapper);
                for(SysMenu sysMenu:sysMenus){
                    //contains---menuIds中是否包含sysMenu.getId
                    if(menuIds.contains(sysMenu.getId())){
                        sysMenu.setChecked(true);
                    }
                }
                List<TreeNode> treeNodes = TreeNode.convertTreeNodes(sysMenus, "");
                return treeNodes;
            }else {
                QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
                wrapper.orderByDesc("id");
                //获取所有菜单
                sysMenus= sysMenuMapper.selectList(wrapper);
                List<TreeNode> treeNodes = TreeNode.convertTreeNodes(sysMenus, "");
                return treeNodes;
            }
        }else{
            QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("id");
            //获取所有菜单
            sysMenus= sysMenuMapper.selectList(wrapper);
            List<TreeNode> treeNodes = TreeNode.convertTreeNodes(sysMenus, "");
            return treeNodes;
        }

    }

    @Override
    public  List<SysMenu> findAllSysMenu() {
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
//        按id降序查询
        wrapper.orderByAsc("id");
        wrapper.eq("menu_type","M").or().eq("menu_type","C");
        //获取所有菜单
        List<SysMenu> sysMenus = sysMenuMapper.selectList(wrapper);
        List<SysMenu> newList = new ArrayList<>();
        for(SysMenu sm:sysMenus){
            //判断是不是上级菜单
            if(sm.getParentId()==0){
                long id= sm.getId();
                List<SysMenu> smChiled = new ArrayList<>();
                //获取该上级菜单的子节点
                for(SysMenu child:sysMenus){
                    if(child.getParentId()==id){
                        smChiled.add(child);
                    }
                }
                sm.setChild(smChiled);
                newList.add(sm);
            }
        }
        return newList;
    }
}
